<%@ page import="com.example.demo_text.CourseDAO" %>
<%@ page import="com.example.demo_text.Exam" %>
<%@ page import="com.example.demo_text.ExamDAO" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String courseId = request.getParameter("course_id");
    
    // 调试输出
    System.out.println("exams.jsp - 接收到的courseId: " + courseId);
    
    String courseName = "";
    if (courseId != null && !courseId.isEmpty()) {
        courseName = CourseDAO.getCourseNameById(courseId);
    }
    
    // Get exams for this course
    List<Exam> exams = null;
    if (courseId != null && !courseId.isEmpty()) {
        exams = ExamDAO.getExamsByCourseId(courseId);
    }
%>
<html>
<head>
    <title>考试管理</title>
    <style>
        /* 如上一致样式 */
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body {
            font-family: 'Segoe UI', sans-serif;
            background: linear-gradient(135deg, #e0f7fa, #f3e5f5);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .card {
            background: white;
            width: 90%;
            max-width: 800px;
            padding: 30px 25px;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        h2 {
            text-align: center;
            margin-bottom: 20px;
            color: #374151;
        }
        p {
            text-align: center;
            color: #4b5563;
            margin-bottom: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th, td {
            padding: 10px;
            border-bottom: 1px solid #ddd;
            text-align: center;
        }
        a.btn-back {
            display: inline-block;
            padding: 10px 20px;
            background-color: #f3f4f6;
            color: #374151;
            border-radius: 8px;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        a.btn-back:hover {
            background-color: #d1d5db;
            color: #111827;
        }
        
        /* Form styles */
        .exam-form {
            margin: 20px 0;
            padding: 20px;
            background-color: #f9fafb;
            border-radius: 8px;
        }
        .exam-form h3 {
            margin-bottom: 15px;
            color: #374151;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
        }
        .form-group input, .form-group textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #d1d5db;
            border-radius: 6px;
        }
        .form-group textarea {
            height: 100px;
            resize: vertical;
        }
        button {
            background-color: #3b82f6;
            color: white;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            border-radius: 6px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        button:hover {
            background-color: #2563eb;
        }
        
        /* Status colors */
        .status-active {
            color: green;
        }
        .status-pending {
            color: blue;
        }
    </style>
</head>
<body>

<div class="card">
    <h2>考试管理</h2>
    <% if (!courseName.isEmpty()) { %>
    <p>当前课程名称：<%= courseName %></p>
    <% } else { %>
    <p>未找到课程信息或未指定课程 ID。</p>
    <% } %>

    <!-- 发布考试表单 -->
    <div class="exam-form">
        <h3>发布新考试</h3>
        <form action="PublishExamServlet" method="post">
            <input type="hidden" name="course_id" value="<%= courseId %>"/>
            
            <div class="form-group">
                <label for="exam_title">考试标题：</label>
                <input type="text" id="exam_title" name="exam_title" required/>
            </div>
            
            <div class="form-group">
                <label for="exam_date">考试日期：</label>
                <input type="date" id="exam_date" name="exam_date" required/>
            </div>
            
            <div class="form-group">
                <label for="exam_time">考试时间：</label>
                <input type="time" id="exam_time" name="exam_time" required/>
            </div>
            
            <div class="form-group">
                <label for="exam_duration">考试时长(分钟)：</label>
                <input type="number" id="exam_duration" name="exam_duration" min="1" required/>
            </div>
            
            <div class="form-group">
                <label for="exam_description">考试说明：</label>
                <textarea id="exam_description" name="exam_description"></textarea>
            </div>
            
            <button type="submit">发布考试</button>
        </form>
    </div>

    <!-- 考试列表 -->
    <div class="module-content">
        <h3>考试列表</h3>
        <table>
            <tr>
                <th>考试名称</th>
                <th>日期</th>
                <th>时间</th>
                <th>时长(分钟)</th>
                <th>状态</th>
            </tr>
            <% if (exams != null && !exams.isEmpty()) { %>
                <% for (Exam exam : exams) { %>
                <tr>
                    <td><%= exam.getExamTitle() %></td>
                    <td><%= exam.getExamDate() %></td>
                    <td><%= exam.getExamTime() %></td>
                    <td><%= exam.getExamDuration() %></td>
                    <td><span class="<%= exam.isActive() ? "status-active" : "status-pending" %>"><%= exam.isActive() ? "已发布" : "待开始" %></span></td>
                </tr>
                <% } %>
            <% } else { %>
                <tr><td colspan="5">暂无考试安排</td></tr>
            <% } %>
        </table>
        <p>考试安排、成绩等相关信息将在教师设置后显示。</p>
    </div>

</div>

</body>
</html>
